/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.wechat.entity.SeckillGainer;
import com.boxin.ims.modules.wechat.service.SeckillGainerService;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 微信秒杀Controller
 * @author Jakemanse
 * @version 2013-09-13
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/seckillGainer")
public class SeckillGainerController extends BaseController {

	@Autowired
	private SeckillGainerService seckillGainerService;
	
	@ModelAttribute
	public SeckillGainer get(@RequestParam(required=false) Long id) {
		if (id != null){
			return seckillGainerService.get(id);
		}else{
			return new SeckillGainer();
		}
	}
	
	@RequiresPermissions("wechat:seckillGainer:view")
	@RequestMapping(value = {"list", ""})
	public String list(SeckillGainer seckillGainer, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if(!user.isAdmin()){
			seckillGainer.setUser(user);
		}
		
		if(seckillGainer.getStartTime() == null){
			Date now = DateUtils.getDayStartTime();
			seckillGainer.setStartTime(now);
			seckillGainer.setCreateTime(now);
		}else{
			seckillGainer.setCreateTime(seckillGainer.getStartTime());
		}
		
        Page<SeckillGainer> page = seckillGainerService.find(new Page<SeckillGainer>(request, response), seckillGainer); 
        model.addAttribute("page", page);
        model.addAttribute("seckillGainer", seckillGainer);
		return "modules/wechat/seckillGainerList";
	}

	@RequiresPermissions("wechat:seckillGainer:view")
	@RequestMapping(value = "form")
	public String form(SeckillGainer seckillGainer, Model model) {
		model.addAttribute("seckillGainer", seckillGainer);
		return "modules/wechat/seckillGainerForm";
	}

	@RequiresPermissions("wechat:seckillGainer:edit")
	@RequestMapping(value = "save")
	public String save(SeckillGainer seckillGainer, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, seckillGainer)){
			return form(seckillGainer, model);
		}
		seckillGainerService.save(seckillGainer);
		addMessage(redirectAttributes, "保存微信秒杀'" + seckillGainer.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/seckillGainer/?repage";
	}
	
	@RequiresPermissions("wechat:seckillGainer:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		seckillGainerService.delete(id);
		addMessage(redirectAttributes, "删除微信秒杀成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/seckillGainer/?repage";
	}
	
	
	@RequiresPermissions("wechat:seckillGainer:edit")
	@RequestMapping(value = "update")
	public String updateStatus(Long id,HttpServletRequest request, RedirectAttributes redirectAttributes) {
		String status = request.getParameter("status");
		seckillGainerService.updateStatusById(id, status) ;
		addMessage(redirectAttributes, ("1".equals(status)) ? "核销成功":"作废成功！");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/seckillGainer/?repage";
	}
	
	

}
